package com.time.admin.infra.rpc;

import com.time.admin.infra.entity.UserInfo;
import com.time.auth.api.UserFeignService;
import com.time.auth.entity.AuthAdminUserDTO;
import com.time.auth.entity.AuthUserDTO;
import com.time.auth.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * auth微服务调用类
 *
 * @author: HuangQi
 * @date: Created in 10:37 2025/10/10
 * @description: auth微服务调用
 */
@Component
public class UserRPC {

    @Autowired
    private UserFeignService userFeignService;

    public UserInfo getUserInfo(String userName) {
        AuthUserDTO authUserDTO = new AuthUserDTO();
        authUserDTO.setUserName(userName);

        // 调用远程服务
        Result<AuthUserDTO> result = userFeignService.getUserInfo(authUserDTO);

        // 封装返回结果
        UserInfo userInfo = new UserInfo();

        // 如果调用失败，返回空对象
        if (!result.getSuccess()) {
            return userInfo;
        }

        // 如果调用成功，封装返回结果
        AuthUserDTO data = result.getData();
        userInfo.setUserName(data.getUserName());
        userInfo.setEmail(data.getEmail());
        userInfo.setAvatarUrl(data.getAvatarUrl());
        return userInfo;
    }

    public UserInfo getAdminUserInfo(String userName) {
        AuthAdminUserDTO authAdminUserDTO = new AuthAdminUserDTO();
        authAdminUserDTO.setUserName(userName);

        // 调用远程服务
        Result<AuthAdminUserDTO> result = userFeignService.getAdminUserInfo(authAdminUserDTO);

        // 封装返回结果
        UserInfo userInfo = new UserInfo();

        // 如果调用失败，返回空对象
        if (!result.getSuccess()) {
            return userInfo;
        }

        // 如果调用成功，封装返回结果
        AuthAdminUserDTO data = result.getData();
        userInfo.setUserName(data.getUserName());
        userInfo.setAvatarUrl(data.getAvatarUrl());
        return userInfo;
    }


}
